package com.mengchengnet.chaperonage.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.adapter.DropDownMenuAdapter;


/**
 * Created by Administrator on 16-3-4.
 */
public class DropDownMenu extends RelativeLayout {

    private Context mContext;

    private Drawable mIconUp;
    private Drawable mIconDown;

    private TextView mTextTitle;
    private ImageView mIconView;

    private PopupWindow mPopupWindow;
    private ListView mListView;
    private RelativeLayout mShadowLayout;
    private AdapterView.OnItemClickListener mItemClickListener;
    private DropDownMenuAdapter mDropDownMenuAdapter;

    public DropDownMenu(Context context) {
        super(context);
        init(context, null);
    }

    public DropDownMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    public void setAdapter(DropDownMenuAdapter adapter) {
        mListView.setAdapter(mDropDownMenuAdapter = adapter);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;

        mIconDown = ContextCompat.getDrawable(mContext, R.mipmap.arrow_down);
        mIconUp = ContextCompat.getDrawable(mContext, R.mipmap.arrow_up);

        LayoutInflater.from(mContext).inflate(R.layout.drop_down_menu, this);
        mTextTitle = (TextView) findViewById(R.id.tv_ddm_title);
        mIconView = (ImageView) findViewById(R.id.iv_ddm_arrow);

        mTextTitle.setText(getContentDescription());

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupWindow = inflater.inflate(R.layout.drop_down_menu_popwindow, (ViewGroup) getParent(), false);

        mPopupWindow = new PopupWindow(popupWindow, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);

        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);

        // 不加这个在低版本（测试了 4.1）上会有外部点击事件不会响应的问题
        //noinspection deprecation
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mIconView.setImageDrawable(mIconDown);
            }
        });

        mShadowLayout = (RelativeLayout) popupWindow.findViewById(R.id.rl_menu_shadow);
        mShadowLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });

        mListView = (ListView) popupWindow.findViewById(R.id.lv_menu);
        mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(parent, view, position, id);
                    mIconView.setImageDrawable(mIconDown);
                }
                mTextTitle.setText(mDropDownMenuAdapter.getName(position));
                mPopupWindow.dismiss();
            }
        });

        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                    mIconView.setImageDrawable(mIconDown);
                } else {
                    mPopupWindow.showAsDropDown(DropDownMenu.this);
                    mPopupWindow.setOutsideTouchable(true);
                    mIconView.setImageDrawable(mIconUp);
                }
            }
        });
    }

    public void setOnItemClickListener(final AdapterView.OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    public void setDefaultItem(Object item) {
        int position = mDropDownMenuAdapter.getPosition(item);
        mListView.performItemClick(mListView.getChildAt(position), position, mListView.getItemIdAtPosition(position));
    }
}


